package edu.gd.servlet;

import com.google.gson.Gson;
import edu.gd.dao.UserDao;
import edu.gd.entity.BaseResponse;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet("/user/del/")
public class UserDelServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username=req.getParameter("username");
        int rows= UserDao.delUserByID(username);
        BaseResponse<Integer> baseResponse=new BaseResponse();
        if(rows>0){
            resp.setContentType("application/json;character=utf-8");
            baseResponse.setCode(200);
            baseResponse.setMsg("操作成功");
            baseResponse.setData(rows);
        }
        else{
            baseResponse.setCode(600);
            baseResponse.setMsg("操作失败");
            baseResponse.setData(rows);
        }
        Gson gson=new Gson();
        String json=gson.toJson(baseResponse);
        PrintWriter out=resp.getWriter();
        out.write(json);
    }
}
